<html>
<head><meta charset="utf-8"><title>Safety contract for `ptr::align_offset` · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Safety.20contract.20for.20.60ptr.3A.3Aalign_offset.60.html">Safety contract for `ptr::align_offset`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="202005337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Safety%20contract%20for%20%60ptr%3A%3Aalign_offset%60/near/202005337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Safety.20contract.20for.20.60ptr.3A.3Aalign_offset.60.html#202005337">(Jun 25 2020 at 18:16)</a>:</h4>
<p>I was writing safety comments for <a href="https://github.com/rust-lang/rust/issues/73622">#73622</a> and need to prove safety when using <code>ptr::align_offset</code>. However, I wasn't able to find any information about its safety contract in its documentation. Can someone help me? cc <span class="user-mention" data-user-id="123586">@nagisa</span></p>



<a name="202036079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Safety%20contract%20for%20%60ptr%3A%3Aalign_offset%60/near/202036079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Safety.20contract.20for.20.60ptr.3A.3Aalign_offset.60.html#202036079">(Jun 25 2020 at 23:05)</a>:</h4>
<p>align_offset is not unsafe, though?</p>



<a name="202036098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Safety%20contract%20for%20%60ptr%3A%3Aalign_offset%60/near/202036098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Safety.20contract.20for.20.60ptr.3A.3Aalign_offset.60.html#202036098">(Jun 25 2020 at 23:05)</a>:</h4>
<p><code>align_to</code> is, but that's because it effectively transmutes <code>T</code> to <code>U</code>.</p>



<a name="202036310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Safety%20contract%20for%20%60ptr%3A%3Aalign_offset%60/near/202036310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Safety.20contract.20for.20.60ptr.3A.3Aalign_offset.60.html#202036310">(Jun 25 2020 at 23:08)</a>:</h4>
<p>Oh, I guess you may be referring to the <code>align_offset</code> that <code>ptr::align_offset</code> calls?</p>



<a name="202036464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Safety%20contract%20for%20%60ptr%3A%3Aalign_offset%60/near/202036464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Safety.20contract.20for.20.60ptr.3A.3Aalign_offset.60.html#202036464">(Jun 25 2020 at 23:11)</a>:</h4>
<p>In that case the implementation <a href="https://github.com/rust-lang/rust/blob/master/src/libcore/ptr/mod.rs#L1108">here</a> requires that alignment is a power of two, as this is not checked inside the function.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>